package com.flyme.module.exam.provider.controller;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.exam.client.api.ExamOptRemoteService;
import com.flyme.module.exam.client.entity.ExamOpt;
import com.flyme.module.exam.provider.service.ExamOptService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author flyme
 */
@Slf4j
@Api(tags="题库选项管理")
@RestController
public class ExamOptController extends BaseController<ExamOptService,ExamOpt> implements ExamOptRemoteService {


    @Override
    public ResultBody list(PagerInfo pagerInfo, String examId) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*");
        cq.eq("examId", examId);
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @Override
    public ResultBody delById(@PathVariable Long id) {
        Boolean tag = bizService.removeById(id);
        if(tag){
            return ResultBody.success("操作成功");
        }else{
            return ResultBody.failed("操作失败");
        }
    }

    @Override
    public ResultBody add(ExamOpt examOpt) {
        Integer num = bizService.count(q().eq("examId", examOpt.getExamId()).eq("examOptionCode", examOpt.getExamOptionCode()));
        if(num > 0){
            return ResultBody.failed("该选项编号已存在");
        }
        boolean result = bizService.saveOrUpdate(examOpt);
        return ResultBody.success(result);
    }

    @Override
    public ResultBody edit(ExamOpt examOpt) {
        boolean result = bizService.updateById(examOpt);
        return ResultBody.success(result);
    }

}
